/*
Copyright (C) 2010 Martin Zuber, zuber@centrum.cz

This application is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.

This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with This application; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "feggclockwidget.h"
#include "ui_feggclockwidget.h"

#include <QDebug>
#include <QSettings>

FEggClockWidget::FEggClockWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FEggClockWidget)
{
    ui->setupUi(this);

    loadPresets();

    // set up timers
    m_timer = new QTimer();
    m_timer->setInterval(1000);

    m_alertTimer = new QTimer();
    m_alertTimer->setInterval(300);

    // set up status flags
    m_isRunning = false;
    m_isAlert = false;

    // connect timers' signals to slots
    connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerTimeOut()));
    connect(m_alertTimer, SIGNAL(timeout()), this, SLOT(onAlertTimerTimeOut()));
}

FEggClockWidget::~FEggClockWidget()
{
    // delete timers
    if(m_timer->isActive())
    {
        m_timer->stop();
        delete m_timer;
    }

    if(m_alertTimer->isActive())
    {
        m_alertTimer->stop();
        delete m_alertTimer;
    }
    delete ui;
}

// one second decrement, if the time reaches 00:00:00 the alert is turn on
void FEggClockWidget::onTimerTimeOut()
{
    qDebug() << "The timer has expired";

    QTime curTime = ui->timeEdit->time();

    qDebug() << "Timer event - minut second";
    curTime = curTime.addSecs(-1);
    ui->timeEdit->setTime(curTime);

    // is it time to alert?
    if(curTime.hour() == 0 && curTime.minute() == 0 && curTime.second() == 0)
    {
        m_isRunning = false;
        qDebug() << "ALERT";
        ui->pbClear->setEnabled(true);
        ui->pbStart->setText(tr("Snooze"));
        m_timer->stop();
        m_alertTimer->start();
        m_isAlert = true;
    }
}

/**
  The button `pbStart` can have three stages:
  <ul>
    <li>Start - after click the counter is started</li>
    <li>Stop - after click the counter is stopped</li>
    <li>Snooze - after click the alert is snoozed</li>
  </ul>
  */
void FEggClockWidget::on_pbStart_clicked()
{
    if(m_isAlert) {
        ui->pbStart->setStyleSheet("");
        ui->pbStart->setText("Start");
        m_alertTimer->stop();
        m_isAlert = false;
        return;
    }
    if(m_isRunning)
    {
        // the timer is running
        m_timer->stop();
        ui->pbStart->setText(tr("Start"));
        m_isRunning = false;
        qDebug() << "Timer has been stopped";
        ui->pbClear->setEnabled(true);
    }
    else
    {
        m_lastTime = ui->timeEdit->time();
        m_timer->start();
        ui->pbStart->setText(tr("Stop"));
        m_isRunning = true;
        qDebug() << "Timer has been started";
        ui->pbClear->setEnabled(false);
    }
}

/**
  Only sets the clock to zero time
  */
void FEggClockWidget::on_pbClear_clicked()
{
    ui->timeEdit->setTime(QTime::fromString("00:00:00", "HH:mm:ss"));
}

/**
  Returns the last set time to widget, this can be done during the counting
  */
void FEggClockWidget::on_pbReset_clicked()
{
    ui->timeEdit->setTime(m_lastTime);
}

/**
  Very simple effects (just now only visual) for alert
  */
void FEggClockWidget::onAlertTimerTimeOut()
{
    if(m_alertStyleSwitch)
    {
        ui->pbStart->setStyleSheet("background: red; color: white;");
        qDebug() << "R";
        m_alertStyleSwitch = false;
    }
    else
    {
        ui->pbStart->setStyleSheet("background: yellow; color: black;");
        qDebug() << "Y";
        m_alertStyleSwitch = true;
    }

    qDebug() << "Alert screen updated" << m_alertStyleSwitch;
}

/**
  Stops and resets the clock, primary usage is for application resize
  @see MainWindow
  */
void FEggClockWidget::stopAlertAndReset()
{
    if(m_timer->isActive()) m_timer->stop();
    if(m_alertTimer->isActive()) m_alertTimer->stop();
    m_isAlert = false;
    m_isRunning = false;
    ui->pbStart->setStyleSheet("");    
    ui->pbStart->setText(tr("Start"));
}

/**
  Loads presets from application settings
  @todo replace hardcoded settings to customizable
  */
void FEggClockWidget::loadPresets()
{
    QSettings settings;
    QStringList keys = settings.allKeys();

    if(keys.size() == 0)
    {
        qDebug() << "No presets found";
    }

    ui->cbPreset->addItem(tr("- none -"));
    foreach(QString key, keys)
    {
        if(key.startsWith("Preset"))
        {
            qDebug() << "Loading data for preset: " << key;
            QTime presetValue = settings.value(key).toTime();

            ui->cbPreset->addItem(key.replace(QString("Preset"), QString("")), presetValue);
            qDebug() << "... value is: " << presetValue;
        }
    }
}

void FEggClockWidget::on_cbPreset_currentIndexChanged(int index)
{
    QVariant timeData = ui->cbPreset->itemData(index);
    if(timeData.isNull()) return;

    ui->timeEdit->setTime(timeData.toTime());
}
